class Utils {
  /** 防抖函数 */
  debounce(fn: Function, delay: number) {
    let timer: number | undefined;
    return function(this:any, ...args: Array<any>) {
      const context = this;
      window.clearTimeout(timer);
      timer = window.setTimeout(function() {
        fn.apply(context, args);
        timer = undefined;
      }, delay);
    }
  }
  /** 节流函数 */
  throttle(fn: Function, delay: number) {
    let timer: number | undefined;
    return function(this:any, ...args: Array<any>) {
      const context = this;
      if (timer) return;
      timer = window.setTimeout(()=>{
        fn.apply(context, args);
        timer = undefined;
      }, delay)
    }
  }
  
}

/** 单例 */
const utils = new Utils();

export default utils;